package finman;
import finman.database.*;

import java.util.HashSet;
import java.util.LinkedList;
import java.util.Set;

public class Application {
	private User currentUser;
	private Set<User> users;

	private Set<Bank> banks;
	private Set<Organization> organizations;
	private Set<Contract> contracts;
	private Set<Valuta> currencies;

    Application() {
		try {
			User user = new User("abc", "xxx");
			connectToDatabase(user);
            System.out.println("log in: " + user);
            printAllAccounts();
		} catch(UserNotFoundException e) {
			System.out.println("ERROR: " + e);
		}
	}

	private void connectToDatabase(User user) throws UserNotFoundException {
		//-------------------- fiiling the database --------------------
		users = new HashSet<User>(3);
		users.add(new User("adfgs", "afgasr", User.Permission.full));
		users.add(new User("abc", "xxx", User.Permission.full));
		users.add(new User("ihnb", "347b", User.Permission.full));

		banks = new HashSet<Bank>(3);
		banks.add(new Bank("Privat"));
		banks.add(new Bank("public"));
		banks.add(new Bank("protected"));
		for(Bank bank: banks) {
			bank.openAccount(new Valuta("usd"), -999d);
		}
		//--------------------------------------------------------------
		// trying to find a user
		boolean user_is_exist = false;
		for(User userInBase : users) {
			if((userInBase.getName().equals(user.getName())) &&
			   (userInBase.getPassword().equals(user.getPassword()))) {
			   user_is_exist = true;
			   break;
			}
		}
		if(! user_is_exist)
			throw new UserNotFoundException();
		//------------------------------------------
		// autorization compleated!
		// what next? ...
    }


	public void printAllAccounts() {
        System.out.println("-----Bank-------Number------Balance--Currency");
        for(Bank bank: banks) {
			Set<Schet> accountsInBank = bank.getAccounts();
            for (Schet account : accountsInBank) {
                System.out.printf("%10s  %s  %.2f    %s%n", bank,
                        account.getNumber(), account.getBalance(), account.getValuta());
            }
		}
	}
	
	public static void main(String[] srgs) {
		System.out.println("Starting ...");
		new Application();
	}
}